package com.xianzhiOA.controller;/**
 * Created by Administrator on 2017/4/27.
 */

import com.xianzhiOA.bean.XzPlan;
import com.xianzhiOA.bean.XzRole;
import com.xianzhiOA.bean.XzRolePower;
import com.xianzhiOA.bean.Xzpower;
import com.xianzhiOA.service.XzRolePowerService;
import com.xianzhiOA.service.XzRoleService;
import com.xianzhiOA.service.XzpowerService;
import com.xianzhiOA.util.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.*;

/**
 * 角色的控制层
 *
 * @author 甘汝雷、王诗博
 * @create 2017-04-27 16:06
 **/
@Controller
@RequestMapping("/Role")
public class XzRoleController {
    @Autowired
    private XzRoleService roleService;
    @Autowired
    private XzpowerService powerService;
    @Autowired
    private XzRolePowerService rolePowerService;


    @RequestMapping("addRole.emp")
    public ModelAndView addRole(XzRole role) {
        ModelAndView mv = new ModelAndView("redirect:selRoleAll.emp");
        Map<String,Object> errors = new HashMap<>();
        if (role.getRoleName() != null && role.getRoleName() != ""){
            List<XzRole> list = roleService.selRoleAll(role);
            if (list.isEmpty()){}else errors.put("roleName","角色已存在，请重新输入！");
        }else errors.put("roleName","角色不能为空");
        if (errors.isEmpty()){
            role.setCreateTime(new Date());
            roleService.addRole(role);
        }else {
            mv.addObject("errors",errors);
            mv.addObject("role",role);
            mv.setViewName("backEnd/roleEdit");
        }
        return mv;
    }

    @RequestMapping("selRoleAll.emp")
    public ModelAndView selRoleAll(XzRole role, @RequestParam(defaultValue = "1")int page, @RequestParam(defaultValue = "8") int rows, Date begin,Date end) {
        ModelAndView mv =new ModelAndView("backEnd/roleShow");
        PageBean<XzRole> pageBean = roleService.selRoleAll(role, page, rows, begin, end);
        List<XzRole> roleList = pageBean.getList();
        mv.addObject("roleList",roleList);
        mv.addObject("role",role);
        mv.addObject("begin", begin);
        mv.addObject("end", end);
        mv.addObject("page", pageBean.getPageNum());
        mv.addObject("pages", pageBean.getPages());
        mv.addObject("rows", pageBean.getPageSize());
        mv.addObject("total", pageBean.getTotal());
        return mv;
    }
    @RequestMapping("roleEdit.emp")
    public ModelAndView roleEdit(Long id){
        ModelAndView mv = new ModelAndView("backEnd/roleEdit");
        if (id != null && id != 0) {
            XzRole role = roleService.selByRoleId(id);
            mv.addObject("role", role);
        }else mv.addObject("role",new XzRole());
        return mv;
    }
    @ResponseBody
    @RequestMapping("selByRoleId.emp")
    public XzRole selByRoleId(Long roleId) {
        return null;
    }

    @RequestMapping("updateRole.emp")
    public ModelAndView update(XzRole role) {
        ModelAndView mv = new ModelAndView("redirect:selRoleAll.emp");
        roleService.update(role);
        return mv;
    }

    @RequestMapping("delete.emp")
    public ModelAndView delete(Long id) {
        ModelAndView mv = new ModelAndView("redirect:selRoleAll.emp");
        XzRole role = new XzRole();
        role.setRoleId(id);
        role.setIsdelete(1);
        roleService.update(role);
        return mv;
    }

    //跳到为角色分配权限页面
    @RequestMapping("goPowerAllotPage.emp")
    public ModelAndView goPowerAllotPage(Long roleId){
         ModelAndView mv=new ModelAndView("backEnd/PowerAllot");
         List<Xzpower> xzpowerList= powerService.selAll();
         //查询出该角色已有的权限
        List<XzRolePower> xzRolePowerList=rolePowerService.selectByRoleId(roleId);

//        List selectPowerIdList=new ArrayList<String>();
        String selectPowerId=";";
       for (int i=0;i<xzRolePowerList.size();i++){
           Xzpower xzpower=powerService.selectByPrimaryKey(xzRolePowerList.get(i).getPowerId());
//           selectPowerIdList.add(xzpower.getPowerNo());
           selectPowerId=selectPowerId+xzpower.getPowerNo()+";";
       }


         mv.addObject("selectPowerId",selectPowerId);
         mv.addObject("xzpowerList",xzpowerList);
         mv.addObject("roleId",roleId);
         return mv;
    }

    //为角色分配权限的方法(添加或修改方法)
    @RequestMapping("powerAllotPage.emp")
    public ModelAndView powerAllotPage(XzRole xzRole){
        ModelAndView mv=new ModelAndView("redirect:selRoleAll.emp");
        Date nowDate=new Date();
        //循环删除

            for (int i = 0; i <= xzRole.getSeletedIdList().size(); i++) {
                XzRolePower xzRolePowerforDel2 = new XzRolePower();
                xzRolePowerforDel2.setRoleId(xzRole.getRoleId());
                xzRolePowerforDel2.setDeleteTime(nowDate);
                //添加或修改前先根据角色ID和权限ID先删除
                int h = rolePowerService.phoneyDelete(xzRolePowerforDel2);
            }

            //循环添加
            for (int i = 0; i < xzRole.getSeletedIdList().size(); i++) {
                XzRolePower xzRolePower = new XzRolePower();
                Xzpower power = powerService.seleByPowerNo(xzRole.getSeletedIdList().get(i).toString());
                xzRolePower.setPowerId(power.getPowerId());
                xzRolePower.setRoleId(xzRole.getRoleId());
                //添加或修改
                int k = rolePowerService.insertRoleAndPower(xzRolePower);
            }


        return mv;
    }

}
